(function(){
    angular.module('app').controller("dspSkuReportCtrl",function($scope,proxy){
        $scope.platformList = [{name:'全部平台'},{id:'android',name:'Android'},{id:'ios',name:'iOS'}]
        $scope.chargeTypeList = [{name:'全部资源类型'},{id:'cpc',name:'CPC'},{id:'cpm',name:'CPM'}]
        $scope.dealTypeList = [{name:'全部采买类型'},{id:'pdb',name:'保价保量'},{id:'pd',name:'保价不保量'}]
        $scope.filter = {};
        $scope.sTime = getNowFormatDate() + " 00:00:00";
        $scope.eTime = getNowFormatDate() + " 23:59:59";
        $scope.chooseDate = function (start, end) {
            if(start) {
                $scope.sTime = start + " 00:00:00";
                $scope.eTime = end + " 23:59:59";
            } else {
                $scope.sTime = $scope.eTime = undefined;
            }
            getList();
            getSum();
        }
        $scope.search = function(val) {
            $scope.filter.skuName = val;
            getList();
            getSum();
        }
        $scope.selectAgent = function(item) {
            $scope.filter.owner = item.id;
            getList();
            getSum();
        }
        $scope.selectPlatform= function(item) {
            $scope.filter.platform = item.id;
            getList();
            getSum();
        }
        $scope.selectChargeType = function(item) {
            $scope.filter.chargeType = item.id;
            getList();
            getSum();
        }
        $scope.selectDealType = function(item) {
            $scope.filter.dealType = item.id;
            getList();
            getSum();
        }
        // 下载报表
        $scope.download = function() {
            // 获得全部数据
            var data = {
                'page': "1",
                'size': "99999",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/dsp/deal/sku',
                data: data,
                success: function(res){
                    var arr = res.data.data || [];
                    arr.forEach(function (v) {
                        v.dealType = v.dealType == 'pdb'?'保价保量':'保价不保量';
                        v.startDate = v.startDate.replace(' 00:00:00','').replace(' 00:00:00','');
                        v.dspProfitRate = v.dspProfitRate ? (v.dspProfitRate*100).toFixed(2) + '%':'';
                        v.clickRate = (v.clickRate*100).toFixed(2) + '%';
                    })

                    exportCsv({
                        title: ['合约资源名称','采买类型','平台类型','资源类型','投放时段','花费(元)','成本(元)','利润(元)','利润率','展示量','点击量','点击率','CPM单价(元)','CPC单价(元)'],
                        titleForKey: ['skuName','dealType','platformTag','chargeType','startDate','advertiserCost','dspCost','dspProfit','dspProfitRate','view','click','clickRate','cpm','cpc'],
                        data: arr,
                    },"合约资源报表.csv");
                }
            })
        }

        getList();
        getSum();
        getAgent();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                'filter': $scope.filter,
                "sort": ["cTime,1"],
                sTime : $scope.sTime,
                eTime : $scope.eTime
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/dsp/deal/sku',
                data: data,
                success: function(res){
                    // 获得列表
                    $scope.list = res.data.data;

                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }

        function getAgent() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/agency/list',
                load: true,
                data: {
                    'page': "1",
                    'size': "999999",
                },
                success: function(res){
                    // 获得列表
                    $scope.agentList = res.data.data;
                    $scope.agentList.unshift({name:'全部广告公司'})
                }
            })
        }
        function getSum() {
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/dsp/deal/sku/sum',
                load: true,
                data: {
                    'page': "1",
                    'size': "999999",
                    'filter': $scope.filter,
                    sTime : $scope.sTime,
                    eTime : $scope.eTime
                },
                success: function(res){
                    // 获得列表
                    $scope.total = res.data.data;
                }
            })
        }
    })
})();